//
//  ContactService.m
//  ImGuider
//
//  Created by llt on 2017/6/10.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "ContactService.h"
#import <WXApi.h>
#import "AccountCell.h"
#import <MessageUI/MessageUI.h>

@interface ContactService ()<MFMailComposeViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UILabel *onlinetimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *currenttimeLabel;
@property (weak, nonatomic) IBOutlet UIView *footView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ContactService

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
}

- (void)dealloc {
    
    [self.timer invalidate];
    self.timer = nil;
}

- (void)setup {
    
    self.title = MYLocalizedString(@"SETTING_CONTACT", nil);
    
    self.label1.text = [MYLocalizedString(@"CONTACT_ONLIETIME", nil) stringByAppendingString:@"："];
    self.label2.text = [MYLocalizedString(@"CONTACT_CURRENTTIME", nil) stringByAppendingString:@"："];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.currenttimeLabel.text = [[NSDate date] beijingTimeStringWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(reloadTime) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    if ([AppManager sharedManager].version.info.online) {
        self.onlinetimeLabel.text = [AppManager sharedManager].version.info.online;
    }
    
    self.dataSource = [NSMutableArray array];
    
    for (NSDictionary *dict in [AppManager sharedManager].version.info.kfs) {
        
        NSDictionary *myDict = @{@"image": [NSString stringWithFormat:@"%@_ service",dict[@"kftype"]], @"account":dict[@"kfaccount"], @"type":dict[@"kftype"], @"name":dict[@"kfname"]};
        [self.dataSource addObject:myDict];
    }
    
    
    CGRect tbfFrame = self.footView.frame;
    tbfFrame.size.height = SCREEN_HEIGHT - (64 + 9) * self.dataSource.count - kNavigationBarHeight - 9;
    
    self.footView.frame = tbfFrame;
    
}

- (void)reloadTime {
    
    self.currenttimeLabel.text = [[NSDate date] beijingTimeStringWithFormat:@"yyyy-MM-dd HH:mm:ss"];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSDictionary *dict = self.dataSource[indexPath.section];
    [cell configWithDict:dict];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dict = self.dataSource[indexPath.section];
    
    NSString *contact = dict[@"account"];
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = contact;
    
    [IGUtils showToast:MYLocalizedString(@"CONTACT_COPY_SUCCESS", nil)];
}


- (void)sendEmailAction:(NSString *)contact {
    
    // 邮件服务器
    MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
    // 设置邮件代理
    [mailCompose setMailComposeDelegate:self];
    
    // 设置收件人
    [mailCompose setToRecipients:@[contact]];
    // 弹出邮件发送视图
    [self presentViewController:mailCompose animated:YES completion:nil];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError *)error {
    // 关闭邮件发送视图
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
